package com.duobaoyu.dby.supply.biz.service.order.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.OrderProductBO;
import com.duobaoyu.dby.supply.biz.converter.OrderConverter;
import com.duobaoyu.dby.supply.biz.service.order.OrderProductService;
import com.duobaoyu.dby.supply.common.biz.bo.product.OrderProductSellCountBO;
import com.duobaoyu.dby.supply.common.biz.dto.order.OperateRemarkDTO;
import com.duobaoyu.dby.supply.common.biz.dto.order.OrderProductStatisticsDTO;
import com.duobaoyu.dby.supply.db.entity.order.OrderProduct;
import com.duobaoyu.dby.supply.db.repository.order.OrderProductRepository;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 订单商品接口实现
 * @Author yangyong
 * @Date 2023/10/18
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderProductServiceImpl implements OrderProductService{
	private final OrderConverter orderConverter;
	private final OrderProductRepository orderProductRepository;

	@Override
	public boolean saveOperateRemark(OperateRemarkDTO operateRemark){
		return orderProductRepository.update(Wrappers.<OrderProduct>lambdaUpdate()
				.set(OrderProduct::getOperateRemark, operateRemark.getOperateRemark())
				.eq(OrderProduct::getSkuCode, operateRemark.getSkuCode())
				.eq(OrderProduct::getOrderSn, operateRemark.getOrderSn()));
	}

    @Override
    public List<OrderProductSellCountBO> statisticsProductSellCount(OrderProductStatisticsDTO orderProductStatistics) {
		return Optional.ofNullable(orderProductRepository.statisticsProductSellCount(orderProductStatistics)).orElse(Collections.emptyList());
    }

	@Override
	public List<OrderProductBO> listOrderProductInfo(List<String> orderSnList){
		if(CollectionUtils.isEmpty(orderSnList)){
			return Lists.newArrayList();
		}
		List<OrderProduct> orderProductList = orderProductRepository.list(Wrappers.lambdaQuery(OrderProduct.class).in(OrderProduct::getOrderSn, orderSnList));
		return orderConverter.toOrderProducts(orderProductList);
	}
}
